//
//  CycLabel.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/25.
//

#import "CycLabel.h"

#import "YYKit.h"

@implementation CycLabel

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

-(instancetype)init{
    if (self=[super init]) {
        [self setUp];
    }

    return self;
}

-(void)setUp{
    self.fontType = CycLabelFontTypeBody;
    self.colorType = CycLabelColorTypeDefault;
}

- (void)setFontType:(CycLabelFontType)fontType{
    _fontType = fontType;
    switch (fontType) {
        case CycLabelFontTypePrimaryTitle:
            self.font = FONT_PRIMARY_TITLE;
            break;
        case CycLabelFontTypeSubTitle:
            self.font = FONT_SECONDARY_TITLE;
            break;
        case CycLabelFontTypeBody:
            self.font = FONT_BODY_16;
            break;
        case CycLabelFontTypeDesc:
            self.font = FONT_BODY_14;
            break;
        case CycLabelFontTypeSmallDesc:
            self.font = FONT_BODY_14;
            break;
        default:
            break;
    }
}

- (void)setColorType:(CycLabelColorType)colorType{
    _colorType = colorType;
    switch (colorType) {
        case CycLabelColorTypeDefault:
            self.textColor = COLOR_WHITE;
            break;
        case CycLabelColorTypeSecondary:
            self.textColor = COLOR_OFFLINE;
            break;
        case CycLabelColorTypeImportant:
            self.textColor = COLOR_DISABLEED;
            break;
        default:
            break;
    }
}

/// 设置高亮文本
/// @param text 所有文字
/// @param highlightText 需要高亮的文字
- (void)setText:(NSString *)text HighlightText:(NSString *)highlightText{
    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc] initWithString:text];
    attri_str.font = self.font;
    attri_str.color = self.textColor;
    [attri_str setColor:COLOR_BLUE range:[text rangeOfString:highlightText]];
    self.attributedText = attri_str;
}

- (void)setText:(NSString *)text HighlightUserText:(NSString *)usertext action:(void(^)(void))useraction HighlightPrivacyText:(NSString *)privacytext action:(void(^)(void))privacyaction {
    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc] initWithString:text];
    attri_str.alignment = self.textAlignment;
    attri_str.font = self.font;
    attri_str.color = self.textColor;
    [attri_str setTextHighlightRange:[text rangeOfString:usertext] color:COLOR_TOPIC_SELECT backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        useraction();
    }];
    [attri_str setTextHighlightRange:[text rangeOfString:privacytext] color:COLOR_TOPIC_SELECT backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        privacyaction();
    }];

    self.attributedText = attri_str;
}

- (void)setText:(NSString *)text HighlightText:(NSString *)highlightText underline:(BOOL)underline action:(void(^)(void))action {
    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc] initWithString:text];
    attri_str.alignment = self.textAlignment;
    attri_str.font = self.font;
    attri_str.color = self.textColor;
    [attri_str setTextHighlightRange:[text rangeOfString:highlightText] color:COLOR_BLUE backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        action();
    }];
    if(underline){
        [attri_str setUnderlineStyle:NSUnderlineStyleSingle range:[text rangeOfString:highlightText]];
    }
    
    self.attributedText = attri_str;
}

- (void)setText:(NSString *)text HighlightText:(NSString *)highlightText highlightFont:(UIFont*)font underline:(BOOL)underline action:(void(^)(void))action {
    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc] initWithString:text];
    attri_str.alignment = self.textAlignment;
    attri_str.font = self.font;
    attri_str.color = self.textColor;
    [attri_str setTextHighlightRange:[text rangeOfString:highlightText] color:COLOR_BLUE backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        action();
    }];
    if(underline){
        [attri_str setUnderlineStyle:NSUnderlineStyleSingle range:[text rangeOfString:highlightText]];
    }
    
    if(font) {
        [attri_str setFont:font range:[text rangeOfString:highlightText]];
    }
    
    self.attributedText = attri_str;
}

- (void)setText:(NSString *)text HighlightText:(NSString *)highlightText highlightFont:(UIFont*)font underline:(BOOL)underline {
    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc] initWithString:text];
    attri_str.alignment = self.textAlignment;
    attri_str.font = self.font;
    attri_str.color = self.textColor;
    [attri_str setTextHighlightRange:[text rangeOfString:highlightText] color:COLOR_BLUE backgroundColor:[UIColor clearColor] tapAction:nil];
    if(underline){
        [attri_str setUnderlineStyle:NSUnderlineStyleSingle range:[text rangeOfString:highlightText]];
    }
    
    if(font) {
        [attri_str setFont:font range:[text rangeOfString:highlightText]];
    }
    
    self.attributedText = attri_str;
}

- (void)setText:(NSString *)text lineSpacing:(CGFloat)lineSpacing wordSpace:(CGFloat)wordSpace{
    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc] initWithString:text];
    attri_str.font = self.font;
    attri_str.color = self.textColor;
    attri_str.lineSpacing = lineSpacing;
    [attri_str addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:wordSpace] range:NSMakeRange(0, text.length)];
    self.attributedText = attri_str;
}


/// 获取指定宽度的文本高度
/// @param width 文本宽度
-(CGFloat)getLabelHeightWithWidth:(CGFloat)width{
    CGSize size = CGSizeMake(width, CGFLOAT_MAX);
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:self.text?:@""];
    NSRange range = NSMakeRange(0, self.text.length);
    
    [attString addAttributes:@{NSFontAttributeName:self.font} range:range];
//    [attString addAttribute:NSForegroundColorAttributeName value:COLOR_OFFLINE range:range];
    
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:attString];
    return layout.textBoundingSize.height;
}

/// 获取指定宽度的文本高度
/// @param height 文本宽度
-(CGFloat)getLabelWidthWithHeight:(CGFloat)height{
    CGSize size = CGSizeMake(CGFLOAT_MAX, height);
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:self.text?:@""];
    NSRange range = NSMakeRange(0, self.text.length);
    
    [attString addAttributes:@{NSFontAttributeName:self.font} range:range];
//    [attString addAttribute:NSForegroundColorAttributeName value:COLOR_OFFLINE range:range];
    
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:attString];
    return layout.textBoundingSize.width;
}

//MARK: -

/// 返回传入字符串的文本宽度
/// @param value 字符串
/// @param font 字体
/// @param height 文本高度
+ (CGFloat)widthForString:(NSString *)value font:(UIFont*)font andHeight:(CGFloat)height{
    CGSize size=CGSizeMake(CGFLOAT_MAX, height);
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:value];
    NSRange range = NSMakeRange(0, value.length);
    [attString addAttributes:@{NSFontAttributeName:font} range:range];
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:attString];
    return layout.textBoundingSize.width;
    
}

/// 返回传入字符串的文本高度
/// @param value 字符串
/// @param font 字体
/// @param width 文本宽度
+ (CGFloat)heightForString:(NSString *)value font:(UIFont*)font andWidth:(CGFloat)width{
    CGSize size=CGSizeMake(width, CGFLOAT_MAX);
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:value];
    NSRange range = NSMakeRange(0, value.length);
    [attString addAttributes:@{NSFontAttributeName:font} range:range];
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:attString];
    return layout.textBoundingSize.height;
}
- (void)setText:(nonnull NSString *)text HighlightText:(nonnull NSString *)highlightText action:(nonnull void (^)(void))action {
}

@end
